<?php

namespace app\api\controller;

use app\api\model\Message;
use app\api\model\Paylog;
use app\api\model\Resource;
use app\api\model\Users;
use think\Exception;
use think\Request;

class User extends Authorization
{
    public function getUserInfo(Request $request)
    {
        $userInfo = (new Users())->find($request->user->user_id);
        $curentIntegralLog = (new Paylog())->where('users_id',$userInfo['id'])->whereTime('create_time','m')->sum('value');
        $data = [
            'username'    =>    $userInfo['username'],
            'nickname'    =>    $userInfo['nickname'],
            'qq'          =>    $userInfo['qq'],
            'sex'         =>    $userInfo['sex'],
            'team'        =>    $userInfo['team'],
            'integral'    =>    $userInfo['integral'],
            'isBindQq'    =>    (!empty($userInfo['qq_token']) || $userInfo['qq_token'] !== 0) ? true : false,
            'avatar'      =>    "http://q.qlogo.cn/g?b=qq&nk=".$userInfo['qq']."&s=640",
            'residue'     =>    "[ ".((int)$this->settings['user_month_integral_limit'] - $curentIntegralLog) . " / ".$this->settings['user_month_integral_limit']." ]",
            'useIntegral' =>    $curentIntegralLog
        ];
        return $this->success('获取成功！',['userInfo'=>$data]);
    }
    public function getProfileDonateLogs(Request $request){
        $res = [];
        $resource = (new Resource())->select();
        $donate = new \app\api\model\Donate();
        foreach ($resource as $k=>$v){
            $line = [];
            $count = $donate->where('status',1)->where(['users_id'=>$request->user->user_id,'resource_id'=>$v['id']])->sum('total');
            $line['name'] = $v['name'];
            $line['total'] = $count;
            $res['resource'][] = $line;
        }
        $res['totalIntegral'] = $donate->where(['users_id'=>$request->user->user_id,'status'=>1])->sum('value');
        return $this->success('ok',$res);
    }
    public function editProfile(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $params['sex'] = AttrToSexValue($params['sex']);
            $params['team'] = AttrToTeamValue($params['team']);
//            验证数据
            $sql = $this->doEditProfile($request->user,$params);
            if(!$sql) return $this->error($sql);

            return $this->success('修改成功');
        }
    }
    private function doEditProfile($user,$params){
        try{
            (new Users())->where('id',$user->user_id)->update($params);
            return true;
        }catch (Exception $e){
            return $e->getMessage();
        }
    }

    public function getMessage(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',15);
        $model = new Message();

        $count = $model->where('users_id',$request->user->user_id)->count();
        $log = $model->where('users_id',$request->user->user_id)->order('create_time','desc')->page($page,$limit)->select();
        return $this->success('ok',['data'=>$log,'count'=>$count]);
    }
    private function TransferData($arr){

    }
}